/*
 * validateSampleEntry.js
 * v0.02
 * 3/23/2011
 *
 * Form validation for creating sample detail.
 */

function validateSampleEntry(){
	var flag = 0;
	var samplecount 	=  document.getElementById('SampleCount');
	var ClientName 	=  document.getElementById('ClientName');
	var ClientCompany	=  document.getElementById('ClientCompany');
	var ClientAddress 		=  document.getElementById('ClientAddress');
	var ClientTel	 	=  document.getElementById('ClientTel');
	var ClientFax 	=  document.getElementById('ClientFax');
	var ClientEmail 	=  document.getElementById('ClientEmail');
	var ClientCell 	=  document.getElementById('ClientCell');

	/*checks individual fields if the input is valid */
	if(checkNumber(samplecount))	{printError("SampleCount",1);}else {flag++;printError("SampleCount",-1);}
	if(checkName(ClientName)) 		{printError("ClientName",1);}else {flag++;printError("ClientName",-1);}
	if(checkName(ClientCompany)) 	{printError("ClientCompany",1);}else {flag++;printError("ClientCompany",-1);}
	if(checkAddress(ClientAddress))	{printError("ClientAddress",1);}else {flag++;printError("ClientAddress",-1);}
	if(checkContacts(ClientFax))	{printError("ClientFax",1);}else {flag++;printError("ClientFax",-1);}
	if(checkContacts(ClientTel))	{printError("ClientTel",1);}else {flag++;printError("ClientTel",-1);}
	if(checkEmail(ClientEmail))		{printError("ClientEmail",1);}else {flag++;printError("ClientEmail",-1);}
	if(checkContacts(ClientCell))		{printError("ClientCell",1);}else {flag++;printError("ClientCell",-1);}
	
	if(flag == 8) return true;

	return false;
}

function checkName(elem){
	var ck_name = /^[A-Za-z0-9,. ]{3,20}$/;
	
	if(ck_name.test(elem.value)) return false;
	
	return true;
}

function checkNumber(elem){
	var ck_num = /^[0-9]{1,15}$/;

	if(ck_num.test(elem.value)) return false;
	
	return true;
}

function checkEmail(elem){
	var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

	if(ck_email.test(elem.value)) return false;
	
	return true;
}

function checkAddress(elem){
	var ck_ad = /^[A-Za-z0-9,.# ]{6,30}$/;
	
	if (ck_ad.test(elem.value)) return false;
	
	return true;	
}

function checkContacts(elem){
	var ck_con = /^[A-Za-z0-9,.() ]{0,30}$/;
	
	if (ck_con.test(elem.value)) return false;
	
	return true;	
}

function printError(elem,x){		
	var str = "";
	
	switch(x){
		case 0: str = "This field is required."; break;
		case 1: str = "Invalid " + elem + "."; break;
		default: str="";
	}	
	document.getElementById("error"+elem).innerHTML = str;		
}
